import request from '@/utils/request'
// 获取频道
export const getChannelAction = (payload) => {
  return async (dispatch, getState) => {
    const { data: { channels } } = await request.get('/channels')
    console.log('频道列表数据：', channels)
    dispatch({ type: 'article/channel', list: channels })
  }
}

// 根据过滤条件获取文章列表
export const getArticleAction = (payload) => {
  return async (dispatch, getState) => {
    const { data: { page, per_page, results, total_count } } = await request.get('/mp/articles', {
      params: payload
    })
    const datas = {
      page, // 当前页码
      pageSize: per_page, // 每页多少条数据
      list: results.map(item => ({
        ...item,
        // 处理cover字段为图片地址，作为列表的封面图
        cover: item.cover.images[0]
      })), // 列表
      total: total_count // 文章数据的总数
    }
    console.log('根据过滤条件获取文章列表：', datas)
    dispatch({ type: 'article/list', datas })
  }
}